package com.appspot.aliga.presentation;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.appspot.aliga.League;
import com.appspot.aliga.R;

public abstract class AligaMainView extends ListActivity {
	
	private static final int LEAGUE_DIALOG = 1;
	
	private League currentLeague;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Bundle bundle = this.getIntent().getExtras();
		int leagueID = bundle.getInt(AppKeys.KEY_LEAGUE_ID);
		this.currentLeague = League.forID(leagueID);
		
		TextView breadCrumb = (TextView) this.findViewById(R.id.breadCrumb);
		breadCrumb.setText(this.currentLeague.name());

		// Buttons
		Button button = (Button) this.findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog(LEAGUE_DIALOG);
			}
		});
		
		Button tableButton = (Button) this.findViewById(R.id.button3);
		tableButton.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				Bundle bundle = new Bundle();
				bundle.putInt(AppKeys.KEY_LEAGUE_ID, getCurrentLeague().getId());
				Intent showTableView = new Intent(view.getContext(), LeagueTableView.class);
				showTableView.putExtras(bundle);
				startActivityForResult(showTableView, 0);
			}
		});
	}

	public League getCurrentLeague() {
		return currentLeague;
	}

	public void setCurrentLeague(League currentLeague) {
		this.currentLeague = currentLeague;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		if (id == LEAGUE_DIALOG) {
			return new LeagueSelection(this);
		}

		return null;
	}
}